﻿Public Class frmPaketScan

    Private WithEvents BC As clsSymbolScanner
    Private PTS As New PaketTrackerService.Webservices

    Private Sub Barcode_Scanned(ByVal bstrBarCode As String, ByVal bstrSource As String, ByVal lStatus As Integer, ByVal lLabelType As Integer, ByVal lDataLength As Integer) Handles BC.ScanComplete

        If bstrBarCode.Trim = "" Then
            MsgBox("Bitte scannen Sie eine gültige Paket-Ident !", MsgBoxStyle.Exclamation)
            Exit Sub
        End If

        Try
            Cursor.Current = Cursors.WaitCursor

            Dim PS As PaketTrackerService.PaketStatus
            clsData.szPaketIdent = bstrBarCode

            '3ICK920PFOISXO3   
            PS = PTS.GetPaketStatus(bstrBarCode)
            If IsNothing(PS) Then
                MsgBox("Ungültige Paket-Ident", MsgBoxStyle.Exclamation)
                Exit Sub
            End If

            'Adresse ermitteln
            clsData.Adresse = PTS.GetAdresse(bstrBarCode, PaketTrackerService.AdresseType.Empfaenger)

            'clsData.Mitarbeiter = PS.MitarbeiterID
            clsData.gdFahrzeug = PS.StationID
            clsData.gdStation = PS.StationID
            clsData.szBeschreibung = PS.Beschreibung

            'Validierungen abhängig des gewählten Menüpunktes
            If fnValidatePaketStatus(PS.Status) Then

                Me.Label2.Text = "Empfänger"
                lblPaketIdent.Text = clsData.szPaketIdent
                lblName.Text = clsData.Adresse.Vorname & " " & clsData.Adresse.Name
                lblOrt.Text = clsData.Adresse.Strasse & " " & clsData.Adresse.Hausnummer
                lblPlZ_Ort.Text = clsData.Adresse.PLZ & " " & clsData.Adresse.Ort

                Me.cmdUnterschrift.Enabled = True
            End If

        Catch ex As Exception
            Cursor.Current = Cursors.Default
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        Finally
            Cursor.Current = Cursors.Default
        End Try

    End Sub

    Private Sub frmPaketScan_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated

        Me.Text = clsGlobals.eOperation.ToString

        If IsNothing(BC) Then BC = New clsSymbolScanner
        BC.Enabled = True

        subClearFields()

    End Sub

    Private Sub subClearFields()

        Me.Label2.Text = "Paket scannen"
        Me.lblName.Text = ""
        Me.lblOrt.Text = ""
        Me.lblPaketIdent.Text = ""
        Me.lblPlZ_Ort.Text = ""
        Me.cmdUnterschrift.Enabled = False

        Select Case clsGlobals.eOperation
            Case clsGlobals.OperationStatus.Annahme
                Me.cmdUnterschrift.Text = "Speichern"
            Case clsGlobals.OperationStatus.Verladung
                Me.cmdUnterschrift.Text = "Speichern"
            Case clsGlobals.OperationStatus.Zentrale
                Me.cmdUnterschrift.Text = "Speichern"
            Case clsGlobals.OperationStatus.Zusteller
                Me.cmdUnterschrift.Text = "Unterschrift"
        End Select

    End Sub

    Private Sub cmdZurueckShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdZurueckShow.Click

        clsGlobals.ShowForm(Me, frmHauptmenue)

    End Sub

    Private Sub frmPaketScan_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        e.Cancel = True
        cmdZurueckShow_Click(sender, e)
    End Sub

    Private Sub frmPaketScan_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate

        If Not IsNothing(BC) Then BC.Enabled = False
        If Not IsNothing(BC) Then BC.Dispose()
        BC = Nothing

    End Sub

    Private Function fnValidatePaketStatus(ByVal Status As PaketTrackerService.PaketStatusType)

        'Prozess	Status vorher (Prüfung)	Status nachher (Web-Aufruf)
        'Annahme	NULL	                0
        'Zentrale	0	                    1
        'Verladung	1	                    2
        'Zusteller	2	                    3

        Try
            Select Case clsGlobals.eOperation
                Case clsGlobals.OperationStatus.Annahme
                    '0 = Aufgenommen in der Datenbank
                    If Not Status = Nothing Or Status <> PaketTrackerService.PaketStatusType.InVersandprozessAufgenommen Then
                        Throw New Exception("Das Paket hat den falschen Status : " & Status.ToString)
                    End If
                Case clsGlobals.OperationStatus.Verladung
                    If Status <> PaketTrackerService.PaketStatusType.ImVersandprozess Then
                        Throw New Exception("Das Paket hat den falschen Status : " & Status.ToString)
                    End If
                Case clsGlobals.OperationStatus.Zentrale
                    If Status <> PaketTrackerService.PaketStatusType.InVersandprozessAufgenommen Then
                        Throw New Exception("Das Paket hat den falschen Status : " & Status.ToString)
                    End If
                Case clsGlobals.OperationStatus.Zusteller
                    If Status <> PaketTrackerService.PaketStatusType.InAuslieferung Then
                        Throw New Exception("Das Paket hat den falschen Status : " & Status.ToString)
                    End If
            End Select
            Return True
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            Return False
        End Try

    End Function

    Private Function fnPostPaketIdent() As Boolean

        'Prozess	Status vorher (Prüfung)	Status nachher (Web-Aufruf)
        'Annahme	NULL	                0
        'Zentrale	0	                    1
        'Verladung	1	                    2
        'Zusteller	2	                    3

        Try
            Select Case clsGlobals.eOperation
                Case clsGlobals.OperationStatus.Annahme                    
                    PTS.InVersandprozessAufgenommen(clsData.szPaketIdent, clsData.Mitarbeiter.Guid, clsData.gdStation, "Paket-Annahme")
                Case clsGlobals.OperationStatus.Verladung                    
                    PTS.InAuslieferung(clsData.szPaketIdent, clsData.Mitarbeiter.Guid, clsData.gdFahrzeug, "Paket-Speicherung")
                Case clsGlobals.OperationStatus.Zentrale
                    PTS.ImVersandprozess(clsData.szPaketIdent, clsData.Mitarbeiter.Guid, clsData.gdStation, "Paket-Aufnahme")
                Case clsGlobals.OperationStatus.Zusteller
                    clsGlobals.ShowForm(Me, frmUnterschrift)
            End Select
            Return True
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            Return False
        End Try

    End Function

    Private Sub cmdUnterschrift_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUnterschrift.Click

        If fnPostPaketIdent() Then
            If clsGlobals.eOperation <> clsGlobals.OperationStatus.Zusteller Then
                MsgBox("Daten gespeichert!", MsgBoxStyle.Information)
            End If
        End If

    End Sub

    Private Sub frmPaketScan_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If (e.KeyCode = System.Windows.Forms.Keys.Up) Then
            'Up
        End If
        If (e.KeyCode = System.Windows.Forms.Keys.Down) Then
            'Down
        End If
        If (e.KeyCode = System.Windows.Forms.Keys.Left) Then
            'Left
        End If
        If (e.KeyCode = System.Windows.Forms.Keys.Right) Then
            'Right
        End If
        If (e.KeyCode = System.Windows.Forms.Keys.Enter) Then
            'Enter
        End If

    End Sub
End Class